//
//  GYMainFootView.m
//  GoodLuck
//
//  Created by 郭为勇 on 2018/3/9.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYMainFootView.h"
#import "GYGoodsRecommed.h"
#import "GYFootViewCell.h"

static NSString *goodRecommendCellIdentifier = @"goodRecommendCellIdentifier";

@interface GYMainFootView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *shopCollectionView;


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionViewHeightCons;

/** 数据源 */
@property (nonatomic, strong) NSArray *goods_commend_list;

/** 当前cell的最大高度 */
@property (nonatomic, assign) CGFloat maxHeight;
@end

@implementation GYMainFootView

- (void)awakeFromNib {
    [super awakeFromNib];
    [self.shopCollectionView registerNib:[UINib nibWithNibName:@"GYFootViewCell" bundle:nil] forCellWithReuseIdentifier:goodRecommendCellIdentifier];
}

- (void)setGoodsDetails:(NSDictionary *)goodsDetails {
    _goodsDetails = goodsDetails;
    
    NSArray *goods_commend_list = [goodsDetails objectForKey:@"goods_commend_list"];
    
    //设置高度
    self.goods_commend_list = [GYGoodsRecommed loadRecommendData:goods_commend_list];
        
    [self.shopCollectionView reloadData];
}



- (CGFloat)ViewHeight:(NSDictionary *)dic {
    
    NSArray *goods_commend_list = [dic objectForKey:@"goods_commend_list"];
    CGFloat height = 0;
    for (GYGoodsRecommed *model in [GYGoodsRecommed loadRecommendData:goods_commend_list]) {
        CGFloat modelHeight = [model returnModelHeight];
        if (height < modelHeight) {
            height = modelHeight;
        }
    }
    self.maxHeight = height;
    NSInteger row = goods_commend_list.count%2;
    NSInteger index = goods_commend_list.count/2;
    if (row != 0) {
        self.collectionViewHeightCons.constant = height *(index+1) + 10*index + 20;
    }else {
        self.collectionViewHeightCons.constant = height *index + (index-1)*10 + 20;
    }
    
    [self layoutIfNeeded];

    return CGRectGetMaxY(self.shopCollectionView.frame);
}
#pragma mark -- UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.goods_commend_list.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    GYFootViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:goodRecommendCellIdentifier forIndexPath:indexPath];
    cell.goodsRecommed = self.goods_commend_list[indexPath.item];
    //cell.backgroundColor = [UIColor orangeColor];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    GYGoodsRecommed *goods = self.goods_commend_list[indexPath.item];
    self.shopRecommendationClickCallBack(goods.goods_id);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
//    GYGoodsRecommed *goods = self.goods_commend_list[indexPath.item];
//    NSLog(@"==============%f",[goods returnModelHeight]);
    return CGSizeMake((ScreenWidth-30)/2, self.maxHeight);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 10, 0, 10);
}







@end
